<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>简易猜数字游戏</title>
</head>

<body>
  <h2>猜数字游戏(1-100)</h2>
  <input type="number" id="guessInput" min="1" max="100" required>
  <button id="submitButton">提交</button>
  <p id="result"></p >

  <script>
    // 生成1到100之间的随机数作为目标数字
    const targetNumber = Math.floor(Math.random() * 100) + 1;
    const submitButton = document.getElementById('submitButton');
    const resultElement = document.getElementById('result');
    const guessInput = document.getElementById('guessInput');

    submitButton.addEventListener('click', function () {
      const userGuess = parseInt(guessInput.value);
      if (isNaN(userGuess)) {
        resultElement.textContent = '请输入有效的数字';
        return;
      }
      if (userGuess === targetNumber) {
        resultElement.textContent = '猜对了！';
      } else if (userGuess < targetNumber) {
        resultElement.textContent = '太小了!再试一次。';
      } else {
        resultElement.textContent = '太大了!再试一次。';
      }
      guessInput.value = '';
    });
  </script>
</body>

</html>